//
//  PersonChannelViewController.m
//  DemoV2
//
//  Created by 董一龙 on 2022/6/25.
//

#import "PersonChannelViewController.h"

@implementation PersonChannelViewController (CollectionViewDataSource)

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.personChannelModel.allArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section >= self.personChannelModel.allArray.count){
        return 0;
    }
    return self.personChannelModel.allArray[section].count;
}

// cell view
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PersonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PersonCollectionViewCell class]) forIndexPath:indexPath];
    [cell removeFromSuperview];
    
    ItemModel *item = self.personChannelModel.allArray[indexPath.section][indexPath.item];
    
    cell.titleLabel.text = item.title;
    [cell.imageView setImage:kGetImage(item.imagePath)];
    return cell;
}

// header和footer view
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        PersonCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([PersonCollectionHeaderView class]) forIndexPath:indexPath];
        [headerView removeFromSuperview];
        
        // 踩坑，titleLabel不能在这里alloc，不如会反复alloc新Label出现重叠
        [headerView.titleLabel setText:(indexPath.section == 0 ? @"好吃的" : @"不好吃的")];
        return headerView;
    }
    
    return nil;
}

@end
